<?php
/**
 * Library of functions and constants for module basiclti
 *
 * It contains the great majority of functions defined by Moodle
 * that are mandatory to develop a module.
 *
 * @author Jordi Piguillem
 *
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package basiclti
 *
 */


$simplelti_CONSTANT = 371;

/**
 * Given an object containing all the necessary data,
 * (defined by the form in mod.html) this function
 * will create a new instance and return the id number
 * of the new instance.
 *
 * @param object $instance An object from the form in mod.html
 * @return int The id of the newly inserted basiclti record
 **/
function basiclti_add_instance($basiclti) {
    global $DB;

   	$basiclti->timecreated = time();
	$basiclti->timemodified = $basiclti->timecreated;

	return $DB->insert_record("basiclti", $basiclti);

}

/**
 * Given an object containing all the necessary data,
 * (defined by the form in mod.html) this function
 * will update an existing instance with new data.
 *
 * @param object $instance An object from the form in mod.html
 * @return boolean Success/Fail
 **/
function basiclti_update_instance($basiclti) {
	global $DB;

    $basiclti->timemodified = time();
    $basiclti->id = $basiclti->instance;

	if (empty($basiclti->preferwidget)){
		$basiclti->preferwidget = 0;
	}

    return $DB->update_record("basiclti", $basiclti);
}

/**
 * Given an ID of an instance of this module,
 * this function will permanently delete the instance
 * and any data that depends on it.
 *
 * @param int $id Id of the module instance
 * @return boolean Success/Failure
 **/
function basiclti_delete_instance($id) {
	global $DB;

    if (! $basiclti = $DB->get_record("basiclti", array("id" => $id))) {
        return false;
    }

    $result = true;

    # Delete any dependent records here #

    if (! $DB->delete_records("basiclti", array("id" => $basiclti->id))) {
        $result = false;
    }

    return $result;
}

/**
 * Return a small object with summary information about what a
 * user has done with a given particular instance of this module
 * Used for user activity reports.
 * $return->time = the time they did it
 * $return->info = a short text description
 *
 * @return null
 * @TODO: implement this moodle function (if needed)
 **/
function basiclti_user_outline($course, $user, $mod, $basiclti) {
	$return = '';
    return $return;
}

/**
 * Print a detailed representation of what a user has done with
 * a given particular instance of this module, for user activity reports.
 *
 * @return boolean
 * @TODO: implement this moodle function (if needed)
 **/
function basiclti_user_complete($course, $user, $mod, $basiclti) {
    return true;
}

/**
 * Given a course and a time, this module should find recent activity
 * that has occurred in basiclti activities and print it out.
 * Return true if there was output, or false is there was none.
 *
 * @uses $CFG
 * @return boolean
 * @TODO: implement this moodle function
 **/
function basiclti_print_recent_activity($course, $isteacher, $timestart) {
    global $CFG;

    return false;  //  True if anything was printed, otherwise false
}

/**
 * Function to be run periodically according to the moodle cron
 * This function searches for things that need to be done, such
 * as sending out mail, toggling flags etc ...
 *
 * @uses $CFG
 * @return boolean
 **/
function basiclti_cron () {
    global $CFG;

    return true;
}

/**
 * Must return an array of grades for a given instance of this module,
 * indexed by user.  It also returns a maximum allowed grade.
 *
 * Example:
 *    $return->grades = array of grades;
 *    $return->maxgrade = maximum allowed grade;
 *
 *    return $return;
 *
 * @param int $basicltiid ID of an instance of this module
 * @return mixed Null or object with an array of grades and with the maximum grade
 *
 * @TODO: implement this moodle function (if needed)
 **/
function basiclti_grades($basicltiid) {
   return NULL;
}

/**
 * Must return an array of user records (all data) who are participants
 * for a given instance of basiclti. Must include every user involved
 * in the instance, independient of his role (student, teacher, admin...)
 * See other modules as example.
 *
 * @param int $basicltiid ID of an instance of this module
 * @return mixed boolean/array of students
 *
 * @TODO: implement this moodle function
 **/
function basiclti_get_participants($basicltiid) {
    return false;
}

/**
 * This function returns if a scale is being used by one basiclti
 * it it has support for grading and scales. Commented code should be
 * modified if necessary. See forum, glossary or journal modules
 * as reference.
 *
 * @param int $basicltiid ID of an instance of this module
 * @return mixed
 *
 * @TODO: implement this moodle function (if needed)
 **/
function basiclti_scale_used ($basicltiid,$scaleid) {
    $return = false;

    //$rec = get_record("basiclti","id","$basicltiid","scale","-$scaleid");
    //
    //if (!empty($rec)  && !empty($scaleid)) {
    //    $return = true;
    //}

    return $return;
}

/**
 * Checks if scale is being used by any instance of basiclti.
 * This function was added in 1.9
 *
 * This is used to find out if scale used anywhere
 * @param $scaleid int
 * @return boolean True if the scale is used by any basiclti
 *
 */
function basiclti_scale_used_anywhere($scaleid) {
	global $DB;

    if ($scaleid and $DB->record_exists('basiclti', array('grade' => $scaleid))) {
        return true;
    } else {
        return false;
    }
}

/**
 * Execute post-install custom actions for the module
 * This function was added in 1.9
 *
 * @return boolean true if success, false on error
 */
function basiclti_install() {
     return true;
}

/**
 * Execute post-uninstall custom actions for the module
 * This function was added in 1.9
 *
 * @return boolean true if success, false on error
 */
function basiclti_uninstall() {
	return true;
}

/**
 *
 * @TODO: DOC this function
 */
function basiclti_get_basiclti_types(){
	global $DB;
	return $DB->get_records('basiclti_types');
}

/**
 *
 * @TODO: DOC this function
 */
function basiclti_get_types(){
    $types = array();

    $type = new object();
    $type->modclass = MOD_CLASS_ACTIVITY;
	$type->type = "basiclti_group_start";
    $type->typestr = '--'.get_string('basicltiactivities', 'basiclti');
    $types[] = $type;

    $type = new object();
    $type->modclass = MOD_CLASS_ACTIVITY;
	$type->type = "basiclti";
    $type->typestr = get_string('modulename', 'basiclti');
    $types[] = $type;

    $basicltitypes = basiclti_get_basiclti_types();
	if (!empty($basicltitypes)){
		foreach ($basicltitypes as $basicltitype) {
	        $type = new object();
	        $type->modclass = MOD_CLASS_ACTIVITY;
	        $type->type = 'basiclti&amp;type='.urlencode($basicltitype->rawname);
	        $type->typestr = $basicltitype->name;
	        $types[] = $type;
		}
    }

    $type = new object();
    $type->modclass = MOD_CLASS_ACTIVITY;
    $type->type = "basiclti_group_end";
    $type->typestr = '--';
    $types[] = $type;

    return $types;
}
//////////////////////////////////////////////////////////////////////////////////////
/// Any other basiclti functions go here.  Each of them must have a name that
/// starts with basiclti_
/// Remember (see note in first lines) that, if this section grows, it's HIGHLY
/// recommended to move all funcions below to a new "localib.php" file.


?>
